home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4104 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.8 KB  |  96 lines

  1. Path: news.slip.net!not-for-mail
  2. From: terman@slip.net (James L. Terman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Help with managed resource class
  5. Date: Sat, 27 Jan 1996 17:25:45 -0800
  6. Message-ID: <4eej3r$9in@slip.net>
  7. NNTP-Posting-Host: sj-pm1-252.slip.net
  8. NNTP-NEWS-ADMIN: newsadmin@slip.net
  9.  
  10. I was developing a binary tree class with a class called BEntry. Since every 
  11. node of a binary tree is a subtree, I thought it would be best that every node 
  12. owned its subnodes. If a node was deleted, it's subnode would be deleted as 
  13. well. I defined two classes, HeapPtr_BEntry and BEntry:
  14.  
  15. #include <iostream.h>
  16.  
  17. class BEntry;
  18.  
  19. class HeapPtr_BEntry
  20. {
  21.     public:
  22.         HeapPtr_BEntry(BEntry* ptr = NULL) : ptr_(ptr) {}
  23.         
  24.         void deallocate()    { delete ptr_; ptr_ = NULL; }
  25.         ~HeapPtr_BEntry()    { deallocate(); }
  26.  
  27.         HeapPtr_BEntry& operator= (BEntry* ptr)
  28.            { deallocate(); ptr_ = ptr; return *this; }
  29.  
  30.         operator BEntry*() const    { return ptr_; }
  31.         BEntry* operator->() const  { return ptr_; }
  32.         BEntry& operator*() const   { return *ptr_; }
  33.         
  34.         BEntry *grab() { BEntry *old = ptr_; ptr_ = NULL; return old; }
  35.          
  36.     private:
  37.         BEntry *ptr_;
  38.         HeapPtr_BEntry operator= (const HeapPtr_BEntry&); // unimplemented
  39.         HeapPtr_BEntry           (const HeapPtr_BEntry&); // unimplememted
  40. };
  41.  
  42. class BEntry 
  43. {
  44.     private:
  45.         int*                 value_; // pointer to value_
  46.         HeapPtr_BEntry   leftEntry_; // remotely owned left node
  47.         HeapPtr_BEntry  rightEntry_; // remotely owned right node
  48.         BEntry*          prevEntry_; // pointer to parent node
  49.         
  50.         BEntry(const BEntry&);            // make copy constructor and 
  51.                                           // assignment operatore private
  52.         BEntry& operator=(const BEntry&); // so that BEntry is only created 
  53.                                           // dynamically
  54.  
  55.     public:    
  56.         BEntry()            : prevEntry_(NULL) {}
  57.         BEntry(int* value)  : prevEntry_(NULL), value_(value) {}    
  58.         ~BEntry() {}
  59.         
  60.         void setValue(int* value)    { value_ = value; }
  61.         const int& getValue() const  { return *value_; }        
  62.         
  63.         int  leftEmpty()        { return !leftEntry_; }
  64.         int rightEmpty() const  { return !rightEntry_; }
  65.         int  prevEmpty() const  { return !prevEntry_; }
  66. };
  67.  
  68. Note that HeapPtr is defined through operator overloading so that
  69. functionally it acts just like a pointer. However, it seems to have
  70. trouble with the way 
  71. a pointer is used in a if expression as it is in leftEmpty(), rightEmpty() and
  72. prevEmpty().
  73.  
  74. When these two classes are compiled the compiliers complains:
  75. error:  BEntry::rightEmpty() cannot access
  76.  HeapPtr_BEntry::HeapPtr_BEntry(): private  member
  77.  
  78. Note that this does not happen with leftEmpty which was not declared a
  79. const function to demonstrate what's going wrong. prevEmpty is acting on
  80. an ordinary pointer and does not return an error. There is no way that the
  81. ! operator should 
  82. change the value its acting upon and it certainly does not need to call the 
  83. constructor function. Note this is not just a problem associated with the !
  84. operator, if (rightEntry_) return 1; else return 0; causes the same problem.
  85.  
  86. This is not a disasterous problem. The following definition for rightEmpty
  87. int rightEmpty() const { return rigtEntry_==NULL; }
  88. compiles and works just fine. And of course, making rightEmpty a non-const
  89. function also works. I was just wondering if anybody could explain the 
  90. compiler (MPW 3.2 C++) behavior.
  91. -- 
  92. | James L. Terman                | Science may set limits to know-  |
  93. | terman@slip.net                | ledge, but should not set limits |
  94. | jlterman@aol.com               | to imagination.                  |
  95. | http://www.slip.net/~terman    |               - Bertrand Russell |
  96.